spring整合RabbitMQ | 您所在的位置:网站首页 › rabbitmq confirmcallback › spring整合RabbitMQ |
本文共 12458 字,大约阅读时间需要 41 分钟。 RabbitMQ整合 SpringCloud实战注意一点,在发送消息的时候对template进行配置mandatory=true保证监听有效 生产端还可以配置其他属性,比如发送重试,超时时间、次数、间隔等 消费端核心配置*首先配置手工确认模式,用于ACK的手工处理,这样我们可以保证消息的可靠性送达,或者在消费端消费失败的时候可以做到重回队列、根据业务记录日志等处理 *可以设置消费端的监听个数和最大个数,用于控制消费端的并发情况 @RabbitListener注解的使用消费端监听 @RabbitListener 注解,这个对于在实际工作中非常的好用 *@RabbitListener是一个组合注解,里面可以注解配置( @QueueBinding、@Queue、@Exchange)直接通过这个组合注解一次性搞定消费端交换机、队列、绑定、路由、并且配置监听功能等 注:由于类配置写在代码里非常不友好,所以强烈建议大家使用配置文件配置 rabbitmq-common子项目: 首先创建一个装实体类的包:entity 然后在entity里添加实体类: package com.lihao.rabbitmqcommon.entity;import java.io.Serializable;/** * @author 浩梦溪 * @site www.lihao.com * @company * @create 2019-12-09 15:44 */public class Order implements Serializable { private String id;private String name;public Order() { }public Order(String id, String name) { super();this.id = id;this.name = name;}public String getId() { return id;}public void setId(String id) { this.id = id;}public String getName() { return name;}public void setName(String name) { this.name = name;}}之后在创建一个子项目:rabbitmq-springcloud-consumer 然后添加Pom依赖: xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.2.RELEASE com.lihao rabbitmq-springcloud-consumer 0.0.1-SNAPSHOT rabbitmq-springcloud-consumer Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter com.lihao rabbitmq-common 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-starter-amqp junit junit 4.12 test org.springframework.boot spring-boot-maven-plugin xmlns="http:>在这里要注意版本也就是下面的这天在pom依赖中出现的代码: 2.2.2.RELEASE不同版本效果不一样。 Yml配置: 这里的yml文件后缀不需要改,创建的时候是什么样子就是什么样子。 然后着这个子项目中创建conusmer包: 然后在添加RabbitReceiver: package com.lihao.rabbitmqspringcloudconsumer.conusmer;import com.lihao.rabbitmqcommon.entity.Order;import com.rabbitmq.client.Channel;import org.springframework.amqp.rabbit.annotation.*;import org.springframework.amqp.support.AmqpHeaders;import org.springframework.messaging.Message;import org.springframework.messaging.handler.annotation.Headers;import org.springframework.messaging.handler.annotation.Payload;import org.springframework.stereotype.Component;import java.util.Map;@Componentpublic class RabbitReceiver { @RabbitListener(bindings = @QueueBinding( value = @Queue(value = "queue-1", durable="true"), exchange = @Exchange(value = "exchange-1", durable="true", type= "topic", ignoreDeclarationExceptions = "true"), key = "springboot.*" ) ) @RabbitHandler public void onMessage(Message message, Channel channel) throws Exception { System.err.println("--------------------------------------"); System.err.println("消费端Payload: " + message.getPayload()); Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG); //手工ACK channel.basicAck(deliveryTag, false); } /*** * spring.rabbitmq.listener.order.queue.name=queue-2 spring.rabbitmq.listener.order.queue.durable=true spring.rabbitmq.listener.order.exchange.name=exchange-1 spring.rabbitmq.listener.order.exchange.durable=true spring.rabbitmq.listener.order.exchange.type=topic spring.rabbitmq.listener.order.exchange.ignoreDeclarationExceptions=true spring.rabbitmq.listener.order.key=springboot.** @param order* @param channel* @param headers* @throws Exception*/ @RabbitListener(bindings = @QueueBinding( value = @Queue(value = "${spring.rabbitmq.listener.order.queue.name}", durable="${spring.rabbitmq.listener.order.queue.durable}"), exchange = @Exchange(value = "${spring.rabbitmq.listener.order.exchange.name}", durable="${spring.rabbitmq.listener.order.exchange.durable}", type= "${spring.rabbitmq.listener.order.exchange.type}", ignoreDeclarationExceptions = "${spring.rabbitmq.listener.order.exchange.ignoreDeclarationExceptions}"), key = "${spring.rabbitmq.listener.order.key}" ) ) @RabbitHandler public void onOrderMessage(@Payload Order order, Channel channel, @Headers Map headers) throws Exception { System.err.println("--------------------------------------"); System.err.println("消费端order: " + order.getId()); Long deliveryTag = (Long)headers.get(AmqpHeaders.DELIVERY_TAG); //手工ACK channel.basicAck(deliveryTag, false); } } , object>MainConfig.java: package com.lihao.rabbitmqspringcloudconsumer;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan({ "com.lihao.rabbitmqspringcloudconsumer.*"})public class MainConfig { }然后在创建子项目;rabbitmq-springcloud-producer子项目: 创建子项目之后添加Pom依赖: xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.2.RELEASE com.lihao rabbitmq-springcloud-producer 0.0.1-SNAPSHOT rabbitmq-springcloud-producer Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine com.lihao rabbitmq-common 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-amqp junit junit 4.12 test org.springframework.boot spring-boot-maven-plugin xmlns="http:>然后是Yml配置: spring.rabbitmq.addresses=192.168.93.140:5672spring.rabbitmq.username=guestspring.rabbitmq.password=guestspring.rabbitmq.virtual-host=/spring.rabbitmq.connection-timeout=15000spring.rabbitmq.publisher-confirms=truespring.rabbitmq.publisher-returns=truespring.rabbitmq.template.mandatory=true然后在这个子项目中创建producer包 创建完producer包之后再添加: RabbitSender : package com.lihao.rabbitmqspringcloudproducer.producer;import com.lihao.rabbitmqcommon.entity.Order;import org.springframework.amqp.rabbit.connection.CorrelationData;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.amqp.rabbit.core.RabbitTemplate.ConfirmCallback;import org.springframework.amqp.rabbit.core.RabbitTemplate.ReturnCallback;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.messaging.Message;import org.springframework.messaging.MessageHeaders;import org.springframework.messaging.support.MessageBuilder;import org.springframework.stereotype.Component;import java.util.Map;@Componentpublic class RabbitSender { //自动注入RabbitTemplate模板类 @Autowired private RabbitTemplate rabbitTemplate;//回调函数: confirm确认 final ConfirmCallback confirmCallback = new ConfirmCallback() { @Override public void confirm(CorrelationData correlationData, boolean ack, String cause) { System.err.println("correlationData: " + correlationData); System.err.println("ack: " + ack); if(!ack){ System.err.println("异常处理...."); } } }; //回调函数: return返回 final ReturnCallback returnCallback = new ReturnCallback() { @Override public void returnedMessage(org.springframework.amqp.core.Message message, int replyCode, String replyText,String exchange, String routingKey) { System.err.println("return exchange: " + exchange + ", routingKey: " + routingKey + ", replyCode: " + replyCode + ", replyText: " + replyText); } }; //发送消息方法调用: 构建Message消息 public void send(Object message, Map properties) throws Exception { MessageHeaders mhs = new MessageHeaders(properties); Message msg = MessageBuilder.createMessage(message, mhs); rabbitTemplate.setConfirmCallback(confirmCallback); rabbitTemplate.setReturnCallback(returnCallback); //id + 时间戳 全局唯一 CorrelationData correlationData = new CorrelationData("1234567890"); rabbitTemplate.convertAndSend("exchange-1", "springboot.abc", msg, correlationData); } //发送消息方法调用: 构建自定义对象消息 public void sendOrder(Order order) throws Exception { rabbitTemplate.setConfirmCallback(confirmCallback); rabbitTemplate.setReturnCallback(returnCallback); //id + 时间戳 全局唯一 CorrelationData correlationData = new CorrelationData("0987654321"); rabbitTemplate.convertAndSend("exchange-2", "springboot.def", order, correlationData); } } , object>然后是直接添加 MainConfig : package com.lihao.rabbitmqspringcloudproducer;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan({ "com.lihao.rabbitmqspringcloudproducer.*"})public class MainConfig { }
之后是开始测试: 转载地址:https://blog.csdn.net/hao_dream_xi/article/details/103459576 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解! |
CopyRight 2018-2019 实验室设备网 版权所有 |